<template>
  <div class="login-wrap">
    <el-form label-position="right" :inline="true" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="80px" style="width: 65%;position: relative;left: 0%;" class="demo-ruleForm login-container">
      <h3 class="title">用户注册</h3>
      <el-form-item prop="account" label="用户昵称">
        <el-input type="text" v-model="ruleForm.account" auto-complete="off" placeholder="请输入用户昵称" style="width:200px"></el-input>
      </el-form-item>
      <el-form-item prop="username" label="注册账号">
        <el-input type="text" v-model="ruleForm.username" auto-complete="off" placeholder="请输入注册账号" style="width:200px"></el-input>
      </el-form-item>
      <el-form-item prop="password" label="注册密码">
        <el-input type="password" v-model="ruleForm.password" auto-complete="off" placeholder="请输入注册密码" style="width:200px"></el-input>
      </el-form-item>
      <el-form-item prop="repassword" label="确认密码">
        <el-input type="password" v-model="ruleForm.repassword" auto-complete="off" placeholder="确认密码" style="width:200px"></el-input>
      </el-form-item>
      <el-form-item prop="email" label="邮箱">
        <el-input type="E_mail" v-model="ruleForm.email" auto-complete="off" placeholder="请输入邮箱" style="width:200px"></el-input>
      </el-form-item>
      <el-form-item prop="name" label="姓名">
        <el-input type="text" v-model="ruleForm.name" auto-complete="off" placeholder="请输入用户姓名" style="width:200px"></el-input>
      </el-form-item>
      <el-form-item prop="telphone" label="手机号">
        <el-input type="text" v-model="ruleForm.telphone" auto-complete="off" placeholder="请输入手机号" style="width:200px"></el-input>
      </el-form-item>
      <el-form-item style="width:100%;"> </el-form-item>
      <el-form-item style="width:100%;"> </el-form-item>
    </el-form>
    <div align="center" style="position: relative;bottom: 80px;">
      <el-col :span="12" :offset="6">
        <el-button type="primary" @click="save()">注册</el-button>
        <el-button @click="bcak()">返回</el-button>
      </el-col>
    </div>
  </div>
</template>
<script type="text/ecmascript-6">
export default {
  name: 'register',
  data() {
    //校验器
    var checkUsername = (rules, value, callback) => {
      this.$axios.get("/api/register/findByUsername/" + value).then(Response => {
        if (Response.data.code == 500) {
          callback();
        }
        callback(new Error("账号已存在！"));
      });
    };
    var checkPassword = (rules, value, callback) => {
      if (value !== this.ruleForm.password) {
        callback(new Error("两次输入密码不一致"))
        return false;
      } else {
        callback();
      }
    };
    return {
      //定义loading默认为false
      logining: false,
      //所属组织数组
      organizationData: [],
      positionData: [],
      directorData: [],
      ruleForm: {
        //username和password默认为空
        no: "",
        username: '',
        password: '',
        repassword: '',
        name: '',
        telphone: '',
        email: '',
        //账户（昵称）
        account: '',
        organization: '',
        //责任人
        director: '',
        //所属岗位
        position: '',
        //工资系数
        wage_coefficient: '',
        //启用状态
        status: '',
        code: '',
        randomStr: '',
        codeimg: '',
        role_id:1,
        salt:'',
      },
      //rules前端验证
      rules: {
        username: [{ required: true, message: '请输入账号', trigger: 'blur' },
        { pattern: /^[A-Za-z0-9]{1,20}$/, message: "请输入1~20位英文、数字", trigger: "blur" },
        { validator: checkUsername, trigger: "blur" }
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' },
        { pattern: /^[A-Za-z0-9]{6,20}$/, message: "请输入6~20位英文、数字", trigger: "blur" },
        ],
        repassword: [{ required: true, message: '请确认密码', trigger: 'blur' },
        { pattern: /^[A-Za-z0-9]{6,20}$/, message: "请输入6~20位英文、数字", trigger: "blur" },
        { validator: checkPassword, trigger: "blur" }
        ],
        name: [{ required: true, message: '请输入用户姓名', trigger: 'blur' }],
        telphone: [{ required: true, message: '请输入手机号', trigger: 'blur' },
        {
          pattern: /^1(3\d|47|5((?!4)\d)|7(0|1|[6-8])|8\d)\d{8,8}$/,
          message: '请输入正确的手机号',
          trigger: 'blur'
        }
        ],
        email: [{ required: true, message: '请输入邮箱', trigger: 'blur' },
        {
          pattern: /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/,
          message: '邮箱格式不正确,示例:abc@qq.com',
          trigger: 'blur'
        }
        ],
        account: [{ required: true, message: '请输入账户昵称', trigger: 'blur' },
        { pattern: /^[A-Za-z0-9]{1,20}$/, message: "请输入1~20位英文、数字", trigger: "blur" }
        ]
      }
    }
  },
  // 创建完毕状态(里面是操作)
  created() {
  },
  // 里面的函数只有调用才会执行
  methods: {
    //获取info列表
    save() {
      this.$refs["ruleForm"].validate(valid => {
        if (!valid) {
          this.$message.error("请按要求填写注册表单！");
        } else {
          this.$axios
            .post("/api/register/add", this.ruleForm)
            .then(res => {
              console.log(res);
              if (res.data.code == 200) {
                this.$message({
                  message: "注册成功",
                  type: "success"
                });
                setTimeout(() => {
                  this.$router.push({ path: '/login' })
                }, 20)
              } else {
                this.$message({
                  type: "info",
                  message: res.data.message
                });
              }
            })
            .catch(err => {
              this.$message.error("注册失败，请稍后再试!");
            });
        }
      })
    },
    bcak(){
      this.$router.push({ path: '/login' })
    }
  }
}
</script>

<style scoped>
.login-wrap {
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  padding-top: 10%;
  background-image: url();
  /* background-color: #112346; */
  background-repeat: no-repeat;
  background-position: center right;
  background-size: 100%;
}

.login-container {
  border-radius: 10px;
  margin: 10px auto;
  width: 350px;
  padding: 30px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  text-align: left;
  box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
  border: 1px solid red;
}

.title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}

.remember {
  margin: 0px 0px 35px 0px;
}

.code-box {
  text-align: right;
}

.codeimg {
  height: 40px;
}
</style>
